/**  
 * All rights Reserved, Designed By www.shatayy.com
 * @Title:  ${file_name}   
 * @Package ${package_name}   
 * @Description:    ${todo}(用一句话描述该文件做什么)   
 * @author: 宁波沙塔信息技术有限公司     
 * @date:   ${date} ${time}   
 * @version V1.0 
 * @Copyright: ${year} www.tydic.com Inc. All rights reserved. 
 * 注意：本内容仅限于宁波沙塔信息技术股份有限公司内部传阅，禁止外泄以及用于其他的商业目
 */
package com.nbst.controller.base;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.nbst.comnutil.JsonUtil;
import com.nbst.comnutil.NormalResult;
import com.nbst.comnutil.StringUtil;
import com.nbst.comnutil.Tools;
import com.nbst.model.base.Base;
import com.nbst.model.base.Menu;
import com.nbst.model.base.User;
import com.nbst.service.base.IDeskService;
import com.nbst.service.base.IMenuService;

/**
 * @ClassName: PermissionsController
 * @Description:权限管理Controller
 * @author 兵
 * @date 2017-8-17
 * 
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
	@Autowired
	IMenuService permissionsservice;

	@Autowired
	IDeskService deskservice;

	/**
	 * 
	 * @Title: getDeskPermissionList @Description: 根据用户Id查询桌面菜单 @param @param uId
	 *         用户Id @param @return @return Object @throws
	 */
	//@PostMapping(value = "/getDesktopList.action")
	public Object getDeskPermissionList(Integer uId) {
		User user = (User) SecurityUtils.getSubject().getPrincipal();
		Map<String,Object> map = new HashMap<>(8);
		if(user==null) {
			return null;
		}
		if("admin".equals(user.getAccount())||"system".equals(user.getAccount())) {
			
		}else {
			map.put("uId", user.getId());
		}
		List<Menu> list = permissionsservice.getDeskPermissionList(map);
		return list;
	}

	/**
	 * 
	 * @Title: getUserPermissionList @Description: 根据用户Id查询权限 @param @param
	 *         rId @param @param uId @param @return @return Menu @throws
	 */
	@PostMapping(value = "/getyhdqxList.action")
	public Object getUserPermissionList(Integer uId) {
		Map<String,Object> map = new HashMap<>();
		if (!StringUtils.isEmpty(uId)) {
			map.put("uId", uId);
		}
		List<Menu> list = permissionsservice.findByUid(map);
		return list;
	}

	/**
	 * 
	 * @Title: getRolePermissionList @Description:
	 *         根据角色ID,或用户ID查询角色或用户所拥有的权限(Base) @param @param
	 *         rId @param @return @return Base @throws
	 */
	@PostMapping(value = "/getjsdqxList.action")
	public Object getRolePermissionList(String rId, String uId) {
		User user = (User) SecurityUtils.getSubject().getPrincipal();
		Map<String,Object> map = new HashMap<>();
		if (StringUtil.isEmpty(rId)) {
			map.put("uId", user.getId());
		} else {
			map.put("rId", rId);
		}
		List<Base> list = permissionsservice.getRolePermissionList(map);
		return list;
	}

	/**
	 * 
	 * @Title: setDesk @Description: 权限树选项 @param @param uId 用户ID @param @param ids
	 *         权限ID @param @return @return Object @throws
	 */

	/**
	 * 
	 * @Title: deleteDesk @Description: 删除Desk信息 @param @param
	 *         uId @param @return @return NormalResult @throws
	 */
	@PostMapping(value = "/removeDesk.action")
	public NormalResult deleteDesk(Integer id) {
		return deskservice.deleteByuId(id);
	}

	/**
	 * 
	 * @Title: getPermissionsList @Description: 根据条件查询权限信息 @param @param record
	 *         (record中如果属性sfyx=1则只查看有效权限，如果sfyx=2则只查看被删除权限，如果sfyx=3则查看全部) @param @return @return
	 *         Object @throws
	 */
	@PostMapping(value = "/getMenuList.action")
	public Object getPermissionsList(Menu record, int offset, int row) {
		Map<String,Object> map = Tools.transBean2Map(record);

		int pageNum = offset;
		int pageSize = row;
		Page<User> page = PageHelper.startPage(pageNum, pageSize);
		permissionsservice.findByCondition(map);
		JSONObject json = new JSONObject();
		json.put("total", page.getTotal());
		json.put("rows", page.getResult());
		return json;
	}

	/**
	 * 
	 * @Title: insert @Description:新增权限信息 @param @param record
	 *         Permissions实体类 @param @param session @param @return @return
	 *         NormalResult @throws
	 */
	@PostMapping(value = "/addNew.action")
	public NormalResult insert(Menu record, HttpSession session) {
		return permissionsservice.insertSelective(record);

	}

	/**
	 * 
	 * @Title: update @Description: 根据主键ID修改权限信息 （包括逻辑删除） @param @param record
	 *         Permissions实体类 @param @param session @param @return @return
	 *         NormalResult @throws
	 */
	@PostMapping(value = "/updateMenu.action")
	public NormalResult update(Menu record, HttpSession session) {
		return permissionsservice.updateByPrimaryKeySelective(record);

	}

	/**
	 * 
	 * @Title: delete @Description: 根据主键ID物理删除权限信息 @param @param pId
	 *         主键ID @param @return @return NormalResult @throws
	 */
	@PostMapping(value = "/deleteMenu.action")
	public NormalResult delete(String pId) {
		return permissionsservice.deleteByPrimaryKey(pId);
	}

	@PostMapping("/menuTree.action")
	public Object menuTree(HttpSession session) {
		User user = (User) SecurityUtils.getSubject().getPrincipal();
		Map<String,Object> map = new HashMap<>(8);
		if (!"admin".equals(user.getAccount()) && !"system".equals(user.getAccount())) {
			map.put("uId", user.getId());
			return JsonUtil.menuTree(permissionsservice.findByUid(map));
		} else {
			return JsonUtil.menuTree(permissionsservice.findByCondition(map));
		}

	}

	/**
	 * 
	 * @Title: getDeskList @Description: 根据条件查询Desk @param @param
	 *         record @param @return @return String @throws
	 */
	@PostMapping(value = "/menuDesk.action")
	public String menuDesk(Integer userid, HttpSession session) {
		User user = (User) SecurityUtils.getSubject().getPrincipal();
		Map<String,Object> map = new HashMap<>();
		if (user != null) {
			if (!StringUtils.isEmpty(user.getId())) {
				map.put("id", user.getId());
				map.put("uId", user.getId());
			}
		}
		if ("system".equals(user.getAccount()) || "admin".equals(user.getAccount())
				|| "SYSTEM".equals(user.getAccount()) || "ADMIN".equals(user.getAccount())) {
			// map.remove("uId");
			return JsonUtil.deelMenu(permissionsservice.getDeskPermissionList(map));
		} else {
			return JsonUtil.deelMenu(permissionsservice.findByUid(map));
		}
	}

	/**
	 * 
	 * @Title: getDeskList @Description: 根据条件查询Desk @param @param
	 *         record @param @return @return String @throws
	 */
	@PostMapping(value = "/desk.action")
	public String Desk(String userid, HttpSession session) {
		User user = (User) SecurityUtils.getSubject().getPrincipal();
		Map<String,Object> map = new HashMap<>();
		if (user != null) {
			map.put("userId", user.getId());
		}
		return JsonUtil.deelDesk(deskservice.findByCondition(map));
	}
	
	/**
	 * 获取用户卡片池
	 * @return
	 * Object 
	 * @author King
	 * @date 2019年8月27日
	 */
	@PostMapping(value = "/getCardPool.action")
	public Object getCardPool() {
		User user = (User) SecurityUtils.getSubject().getPrincipal();
		if(user==null) {
			return null;
		}
		Map<String,Object> map = new HashMap<>(8);
		map.put("id",user.getId());
		map.put("fCardPool", "1");
		if ("system".equals(user.getAccount()) || "admin".equals(user.getAccount())
				|| "SYSTEM".equals(user.getAccount()) || "ADMIN".equals(user.getAccount())) {
			return permissionsservice.getDeskPermissionList(map);
		} else {
			map.put("uId", user.getId());
			return permissionsservice.findByUid(map);
		}
	}
	
	
	
	
	

	/**
	 * 新增工作台卡片
	 * 
	 * @param ids
	 *            需要新增的卡片id
	 * @param indexs
	 *            需要新增的卡片排序号
	 * @param session
	 *            void
	 * @author King
	 * @date 2019年8月14日
	 */
	@PostMapping(value = "/setDesk.action")
	public NormalResult setDesk(String ids, HttpSession session, String indexs) {
		NormalResult result = new NormalResult("9999", "操作失败");
		User user = (User) SecurityUtils.getSubject().getPrincipal();
		Map<String,Object> map = new HashMap<>();
		if (user != null) {
			map.put("userId", user.getId());
			deskservice.deleteByuId(user.getId());
		}
		if (!StringUtils.isEmpty(ids)) {
			String[] id = ids.split(",");
			String[] index = indexs.split(",");
			for (int i = 0; i < id.length; i++) {
				map.put("menuId", Integer.valueOf(id[i]));
				map.put("rank", index[i]);
				deskservice.insertSelective(map);
			}
			result.setCode("0000");
			result.setMessage("操作成功");
		} else {
			result.setMessage("请拖动需要放置到工作台的卡片");
		}
		return result;
	}

	/**
	 * 卡片位置设置
	 * 
	 * @param si
	 *            卡片1的id
	 * @param sm
	 *            卡片1的顺序
	 * @param ni
	 *            卡片2的id
	 * @param nm
	 *            卡片2的顺序
	 * @param session
	 *            void
	 * @author King
	 * @date 2019年8月14日
	 */
	@PostMapping(value = "/updateDesk.action")
	public void updateDesk(String si, String sm, String ni, String nm, HttpSession session) {
		User user = (User) SecurityUtils.getSubject().getPrincipal();
		Map<String,Object> map = new HashMap<>();
		int a = 0;
		if (user != null) {
			map.put("userId", user.getId());
		}
		if (!StringUtils.isEmpty(sm)) {
			map.put("menuId", sm);
			map.put("rank", si);
			deskservice.updateByPrimaryKeySelective(map);
		}
		if (!StringUtils.isEmpty(nm)) {
			map.put("menuId", nm);
			map.put("rank", ni);
			deskservice.updateByPrimaryKeySelective(map);
		}
	}

	@PostMapping(value = "/deleteDesk.action")
	public NormalResult deleteDesk(String ids) {
		NormalResult result = new NormalResult("9999", "操作失败");
		User user = (User) SecurityUtils.getSubject().getPrincipal();
		Map<String,Object> map = new HashMap<>();
		if (user != null) {
			map.put("id", user.getId());
			String[] id = ids.split(",");
			if (id.length > 0) {
				map.put("ids", id);
				deskservice.deleteByUserIdAndPid(map);
				result.setCode("0000");
				result.setMessage("操作成功");
			} else {
				result.setCode("0000");
				result.setMessage("没有删除任何卡片");
			}
		} else {
			result.setMessage("登录信息已失效，请重新登录");
		}

		return result;
	}

}